package org.travel_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.travel_system.pojo.Hotel;
import org.travel_system.pojo.Room;
import org.travel_system.utils.MinioUtil;
import org.travel_system.utils.Result;
import org.travel_system.service.HotelService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RestController
@RequestMapping("/hotels")
public class HotelController {

    private final HotelService hotelService;

    @Autowired
    public HotelController(HotelService hotelService) {
        this.hotelService = hotelService;
    }

    @Autowired
    private MinioUtil minioUtil;

    //查询酒店列表
    @GetMapping
    public Result<List<Hotel>> getAllHotels() {
        return Result.success(hotelService.getHotels());
    }

    //获得酒店的封面
    @GetMapping("/images")
    public Result<List<Map<String, String>>> getHotelImagesWithAlt() {
        List<Hotel> hotels = hotelService.getHotelsImage();

        // 使用Stream API将每个酒店对象转换为一个包含名称、image属性和alt属性的Map
        List<Map<String, String>> images = hotels.stream()
                .map(hotel -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("url", hotel.getImage());  // 添加酒店图像URL
                    System.out.println(hotel.getImage());
                    return map;
                })
                .collect(Collectors.toList());
        return Result.success(images);
    }

    //查询酒店通过名字
    @GetMapping("/{name}")
    public Result<Hotel> getHotelByName(@PathVariable String name) {
        return Result.success(hotelService.getHotelByName(name));
    }

    //实现模糊查询酒店
    @GetMapping("/search/{name}")
    public Result<List<Hotel>> getSearchHotels(@PathVariable String name){
        System.out.println(hotelService.getSearchHotelsByName(name));
        return Result.success(hotelService.getSearchHotelsByName(name));
    }

    //删除酒店
    @DeleteMapping("/{name}")
    public Result<?> deleteHotel(@PathVariable String name) {
        if(hotelService.getHotelByName(name) == null){
            return Result.error("酒店不存在");
        }
        hotelService.deleteHotel(name);
        return Result.success();
    }

    //修改酒店，增加封面图片
    @PutMapping
    public Result<?> updateHotel(@RequestBody Hotel hotel) {
        System.out.println(hotel);
        if(hotelService.getHotelByName(hotel.getHotelName()) == null){
            return Result.error("酒店不存在");
        }
        hotelService.updateHotel(hotel);
        return Result.success();
    }

    //添加酒店
    @PostMapping
    public Result<?> addHotel(@RequestBody Hotel hotel) {
        System.out.println(hotel);
        if(hotelService.getHotelByName(hotel.getHotelName())!= null){
            return Result.error("酒店已存在");
        }
        hotelService.addHotel(hotel);
        return Result.success();
    }

    //查询所有房间
    @GetMapping("/rooms")
    public Result<List<Room>> getAllRooms() {
        return Result.success(hotelService.getAllRooms());
    }

    //查询酒店房间列表
    @GetMapping("/rooms/{hotelName}")
    public Result<List<Room>> getRooms(@PathVariable String hotelName) {
        return Result.success(hotelService.getRooms(hotelName));
    }

    //查询单个酒店房间
    @GetMapping("/rooms/{hotelName}/{roomNumber}")
    public Result<Room> getRoomByRoomNumber(@PathVariable String hotelName, @PathVariable int roomNumber) {
        return Result.success(hotelService.getRoomByRoomNumber(hotelName,roomNumber));
    }

    //添加酒店房间
    @PostMapping("/rooms/{hotelName}")
    public Result<?> addRoom(@RequestBody Room room, @PathVariable String hotelName) {
        if(hotelService.getHotelByName(hotelName) == null){
            return Result.error("酒店不存在");
        }
        if(hotelService.getRoomByRoomNumber(hotelName,room.getRoomNumber())!= null){
            return Result.error("房间号已存在");
        }
        room.setHotelName(hotelName);
        return hotelService.addRoom(room) > 0 ?
                Result.success() :
                Result.error("添加房间失败");
    }

    //删除酒店房间
    @DeleteMapping("/rooms/{hotelName}/{roomNumber}")
    public Result<?> deleteRoomByRoomNumber(@PathVariable String hotelName, @PathVariable String roomNumber) {
        return hotelService.deleteRoom(hotelName,roomNumber) > 0 ?
                Result.success() :
                Result.error("删除房间失败");
    }

    //更新酒店房间信息
    @PutMapping("/rooms/{hotelName}/{roomNumber}")
    public Result<?> updateRoom(@PathVariable String hotelName,
                                @PathVariable int roomNumber,
                                @RequestBody Room room) {
        if (hotelService.getHotelByName(hotelName) == null) {
            return Result.error("酒店不存在");
        }
        if (hotelService.getRoomByRoomNumber(hotelName, roomNumber) == null) {
            return Result.error("房间不存在");
        }
        room.setHotelName(hotelName);
        room.setRoomNumber(roomNumber);
        return hotelService.updateRoom(room) > 0 ?
                Result.success() :
                Result.error("更新房间失败");
    }

}
